package com.alipay.pluginmodule;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.alipay.pluginframework.HookManager;
import com.alipay.pluginframework.ProxyActivityInterface;

import java.lang.reflect.Constructor;

import androidx.appcompat.app.AppCompatActivity;

/**
 * @author: tanghaoshu
 * @date: 2021/9/23
 */
public class ProxyActivity extends AppCompatActivity {

    private ProxyActivityInterface pluginObj;

    private Resources mAppResources = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //在这里拿到了真实跳转的activity 拿出来 再去启动真实的activity
        String className = getIntent().getStringExtra("ClassName");
        Log.e("className", "classname --  " + className);
        //通过反射在去启动一个真实的activity 拿到Class对象
        try {
            Class<?> plugClass = getClassLoader().loadClass(className);
            Constructor<?> pluginConstructor = plugClass.getConstructor();
            //因为插件的activity实现了我们的标准
            pluginObj = (ProxyActivityInterface) pluginConstructor.newInstance();

            //注入上下文
            pluginObj.attach(ProxyActivity.this);
            mAppResources = HookManager.getInstance().getResource();
            //一定要调用onCreate
           pluginObj.onCreate(savedInstanceState);

        } catch (Exception e) {
            if (e.getClass().getSimpleName().equals("ClassCastException")) {

                //我这里是直接拿到异常判断的 ，也可的 拿到上面的plugClass对象判断有没有实现我们的接口
                finish();
                Toast.makeText(this, "非法页面", Toast.LENGTH_LONG).show();
                return;
            }
            e.printStackTrace();
        }
    }

    //为什么要重写这个呢 因为这个是插件内部startActivity调用的 将真正要开启的activity的类名传过来
    //然后取出来，启动我们的占坑的activity 在将我们真正要启动的Activity塞进去
    @Override
    public void startActivity(Intent intent) {
        String className1 = intent.getStringExtra("ClassName");
        Bundle bundle = intent.getExtras();
        boolean isPlugin = intent.getBooleanExtra("isPlugin", true);
        Intent intent1 = null;
        try {
            if (isPlugin){
                intent1 = new Intent(this, ProxyActivity.class);
                intent1.putExtra("ClassName", className1);
            }else {
                intent1 = new Intent(this, super.getClassLoader().loadClass(className1));
            }
            intent1.putExtras(bundle);
            super.startActivity(intent1);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    //重写classLoader
    @Override
    public ClassLoader getClassLoader() {
        return HookManager.getInstance().getClassLoader();

    }

    //重写Resource
    @Override
    public Resources getResources() {
        if(mAppResources == null){
            return super.getResources();
        }
        return this.mAppResources;
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (pluginObj!=null){
            pluginObj.onStart();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (pluginObj!=null){
            pluginObj.onDestroy();
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (pluginObj!=null){
            pluginObj.onPause();
        }

    }
}
